package com.hsd.znsh.controller.app;

import com.hsd.znsh.entity.*;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.input.HostSetting;
import com.hsd.znsh.pojo.output.HostGeolocationVO;
import com.hsd.znsh.pojo.output.HostVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.output.SensorVO;
import com.hsd.znsh.service.*;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-28 17:56:02
 */
@RequestMapping("/app/host")
@RestController
public class APPHostController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private HostService hostService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private ControlService controlService;

    @Autowired
    private EventService eventService;


    @GetMapping("/getHostList")
    public ResultVO getUserHostList(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<HostVO>  list=hostService.getHostListByAPPUser(user);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getOwnHostList")
    public ResultVO getOwnUserHostList(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<Host>  list=hostService.getOwnHostListByAPPUser(user);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getManagerHostList")
    public ResultVO getManagerHostList(String managerName){
        List<Host>  list=hostService.getManagerHostList(managerName);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getHostInfoByHostSn")
    public ResultVO getHostInfoByHostSn(String hostSn){
        if(StringUtils.isEmpty(hostSn)) throw new ZnshException("主机编号不能为空");
        Host host=hostService.getHostInfoByHostSn(hostSn);
        return ResultVOUtil.success(host);
    }

    @GetMapping("/getControlAliasListByHostSn")
    public ResultVO getControlAliasListByHostSn(@RequestParam(required = false) String hostSn){
        if(StringUtils.isEmpty(hostSn)) throw new ZnshException("主机编号不能为空");
        List<Control> list=controlService.getControlListByHostSn(hostSn);
        return ResultVOUtil.success(list);
    }

    @PostMapping("/updateCrop")
    public ResultVO updateCrop(String hostSn,String name,String value){
        hostService.updateCrop(hostSn,name,value);
        return ResultVOUtil.success();
    }

    @PostMapping("/updateAlias")
    public ResultVO updateAlias(String type,String sn,String alias){
        commonService.updateAlias(type,sn,alias);
        return ResultVOUtil.success();
    }

    @PostMapping("/updateHostSettingsInfoByHostSn")
    public ResultVO updateHostSettingsInfoByHostSn(@Valid HostSetting hostSetting, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        hostService.updateHostSettingsInfoByHostSn(hostSetting);
        return ResultVOUtil.success();
    }

    @GetMapping("/getEventList")
    public ResultVO getEventList(@RequestHeader("token") String token,Integer readStatus,Integer page){
        User user=redisService.get(token,User.class);
        List<Event> list=eventService.getEventList(user,readStatus,page);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getEventInfo")
    public ResultVO getEventInfo(Integer id){
        Event event=eventService.getEventInfo(id);
        return ResultVOUtil.success(event);
    }

}

